package com.cjh.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;


@RestController
public class HelloController {

    @GetMapping("/helloSecurity")
    public String helloSecurity(){
        return "你好,security";
    }

    /**
     * 第一种：获取当前用户信息
     * 直接从容器中注入进来
     * @param principal
     * @return
     */
    @GetMapping("/getUserInfo1")
    public Principal getUserInfo1(Principal principal){
        return principal;
    }

    /**
     * 第二种：获取当前用户信息
     * 当用户登录后，会把信息放在SecurityContextHolder里面，这是基于session方式的认证
     * @return
     */
    @GetMapping("/getUserInfo2")
    public Authentication getUserInfo2(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }
}
